package com.yunshan.mp.service.impl;

import com.yunshan.common.annotation.DataScope;
import com.yunshan.common.utils.DateUtils;
import com.yunshan.mp.domain.MpServeInfo;
import com.yunshan.mp.domain.MpServeType;
import com.yunshan.mp.domain.ResultServe;
import com.yunshan.mp.mapper.MpServeInfoMapper;
import com.yunshan.mp.service.IMpServeInfoService;
import com.yunshan.mp.service.IMpServeTypeService;
import com.yunshan.order.service.IOrderConsumeRecordService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 服务设置Service业务层处理
 * 
 * @author yunshan
 * @date 2025-01-20
 */
@Service
public class MpServeInfoServiceImpl implements IMpServeInfoService 
{
    @Autowired
    private MpServeInfoMapper mpServeInfoMapper;
    @Autowired
    private IOrderConsumeRecordService orderConsumeRecordService;
    @Autowired
    private IMpServeTypeService mpServeTypeService;

    /**
     * 查询服务设置
     * 
     * @param id 服务设置主键
     * @return 服务设置
     */
    @Override
    public MpServeInfo selectMpServeInfoById(Long id)
    {
        MpServeInfo mpServeInfo = mpServeInfoMapper.selectMpServeInfoById(id);
        mpServeInfo.setPayCount(orderConsumeRecordService.selectServeCount(mpServeInfo.getId()));
        return mpServeInfo;
    }

    /**
     * 查询服务设置列表
     * 
     * @param mpServeInfo 服务设置
     * @return 服务设置
     */
    @Override
    @DataScope(deptAlias = "mp_serve_info")
    public List<MpServeInfo> selectMpServeInfoList(MpServeInfo mpServeInfo)
    {
        List<MpServeInfo> mpServeInfos = mpServeInfoMapper.selectMpServeInfoList(mpServeInfo);
        for (MpServeInfo serveInfo : mpServeInfos) {
            serveInfo.setPayCount(orderConsumeRecordService.selectServeCount(serveInfo.getId()));
        }
        return mpServeInfos;
    }

    /**
     * 新增服务设置
     * 
     * @param mpServeInfo 服务设置
     * @return 结果
     */
    @Override
    public int insertMpServeInfo(MpServeInfo mpServeInfo)
    {
        mpServeInfo.setCreateTime(DateUtils.getNowDate());
        return mpServeInfoMapper.insertMpServeInfo(mpServeInfo);
    }

    /**
     * 修改服务设置
     * 
     * @param mpServeInfo 服务设置
     * @return 结果
     */
    @Override
    public int updateMpServeInfo(MpServeInfo mpServeInfo)
    {
        mpServeInfo.setUpdateTime(DateUtils.getNowDate());
        return mpServeInfoMapper.updateMpServeInfo(mpServeInfo);
    }

    /**
     * 批量删除服务设置
     * 
     * @param ids 需要删除的服务设置主键
     * @return 结果
     */
    @Override
    public int deleteMpServeInfoByIds(Long[] ids)
    {
        return mpServeInfoMapper.deleteMpServeInfoByIds(ids);
    }

    /**
     * 删除服务设置信息
     * 
     * @param id 服务设置主键
     * @return 结果
     */
    @Override
    public int deleteMpServeInfoById(Long id)
    {
        return mpServeInfoMapper.deleteMpServeInfoById(id);
    }

    @Override
    public List<ResultServe> selectResultServeList(MpServeInfo mpServeInfo) {

        List<ResultServe> mpServeInfos = new ArrayList<>();

        //查询服务类型
        List<MpServeType> mpServeTypes = mpServeTypeService.selectMpServeTypeList(new MpServeType());
        mpServeTypes.removeIf(mpServeType -> "0".equals(mpServeType.getType()));

        for (MpServeType mpServeType : mpServeTypes) {
            //根据服务类型查询服务
            mpServeInfo.setServeType(String.valueOf(mpServeType.getType()));
            List<ResultServe> list = mpServeInfoMapper.selectResultServeList(mpServeInfo.getServeType());

            //封装树状结构
            ResultServe resultServe = new ResultServe();
            resultServe.setId(mpServeInfo.getServeType());
            resultServe.setLabel(mpServeType.getLabel());
            resultServe.setChildren(list);
            mpServeInfos.add(resultServe);
        }

        return mpServeInfos;
    }

    @Override
    public List<MpServeInfo> selectUniServeList(String serveType) {
        return mpServeInfoMapper.selectUniServeList(serveType);
    }


}
